//
//  myToolbarController.m
//  iSwitchView
//
//  Created by Ronnie on 08-11-19.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "myToolbarController.h"
#define TOOLBARITEM_HEADIMAGE @"userHeadImage"
#define TOOLBARITEM_STATEINFO @"userStateInfo"

@implementation myToolbarController
- (void) dealloc
{
	[mainAppCtrl release];
	[mainWindowCtrl release];
	[super dealloc];
}


-(void)awakeFromNib
{
    NSToolbar *toolbar=[[[NSToolbar alloc] initWithIdentifier:@"myToolbar"] autorelease];
    [toolbar setDelegate:self];
    [toolbar setAllowsUserCustomization:NO];
	[toolbar setAutosavesConfiguration: YES]; 
    [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
    // install the toolbar.
    [theWindow setToolbar:toolbar];
	[theWindow setShowsToolbarButton:NO];
	[toolbar setVisible:NO];
	
	
	[statesList setMenu:statesMenu];
	
	[[statesList itemAtIndex:1] setTitle:@"available"];
	[[statesList itemAtIndex:1] setImage:[NSImage imageNamed:@"available"]];
	[[statesList itemAtIndex:1] setTarget:self];
	[[statesList itemAtIndex:1] setAction:@selector(onStatusChange:)];
	[[statesList itemAtIndex:1] setState:NSOffState];
	
	
	[[statesList itemAtIndex:3] setTitle:@"busy"];
	[[statesList itemAtIndex:3] setImage:[NSImage imageNamed:@"busy"]];
	[[statesList itemAtIndex:3] setTarget:self];
	[[statesList itemAtIndex:3] setAction:@selector(onStatusChange:)];
	[[statesList itemAtIndex:3] setState:NSOffState];
	
	[[statesList itemAtIndex:4] setTitle:@"meeting"];
	[[statesList itemAtIndex:4] setImage:[NSImage imageNamed:@"meeting"]];
	[[statesList itemAtIndex:4] setTarget:self];
	[[statesList itemAtIndex:4] setAction:@selector(onStatusChange:)];
	[[statesList itemAtIndex:4] setState:NSOffState];
	
	[[statesList itemAtIndex:5] setTitle:@"phonecall"];
	[[statesList itemAtIndex:5] setImage:[NSImage imageNamed:@"phonecall"]];
	[[statesList itemAtIndex:5] setTarget:self];
	[[statesList itemAtIndex:5] setAction:@selector(onStatusChange:)];
	[[statesList itemAtIndex:5] setState:NSOffState];
	
	[[statesList itemAtIndex:7] setTitle:@"away"];
	[[statesList itemAtIndex:7] setImage:[NSImage imageNamed:@"dinner"]];
	[[statesList itemAtIndex:7] setTarget:self];
	[[statesList itemAtIndex:7] setAction:@selector(onStatusChange:)];
	[[statesList itemAtIndex:7] setState:NSOffState];
	
	[[statesList itemAtIndex:8] setTitle:@"dinner"];
	[[statesList itemAtIndex:8] setImage:[NSImage imageNamed:@"dinner"]];
	[[statesList itemAtIndex:8] setTarget:self];
	[[statesList itemAtIndex:8] setAction:@selector(onStatusChange:)];
	[[statesList itemAtIndex:8] setState:NSOffState];
	
	[[statesList itemAtIndex:9] setTitle:@"offsite"];
	[[statesList itemAtIndex:9] setImage:[NSImage imageNamed:@"offsite"]];
	[[statesList itemAtIndex:9] setTarget:self];
	[[statesList itemAtIndex:9] setAction:@selector(onStatusChange:)];
	[[statesList itemAtIndex:9] setState:NSOffState];
	
	[[statesList itemAtIndex:11] setTitle:@"invisible"];
	[[statesList itemAtIndex:11] setImage:[NSImage imageNamed:@"invisible"]];
	[[statesList itemAtIndex:11] setTarget:self];
	[[statesList itemAtIndex:11] setAction:@selector(onStatusChange:)];
	[[statesList itemAtIndex:11] setState:NSOffState];
	
}



- (IBAction)onStatusChange:(id)sender
{
	NSMenuItem* item = (NSMenuItem*)sender;
	/*[statesList setTitle:[item title]];
	[activeItem setState:NSOffState];
	[item setState:NSOnState];
	activeItem = item;
	[stateImage setImage:[NSImage imageNamed:[item title]]];*/
	
	NSLog(@"Width of String is %f", [statesList frame].size.width );
	[self stateChangeTo:[item title]];
}

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
    // We create and autorelease a new NSToolbarItem, and then go through the process of setting up its
    // attributes from the master toolbar item matching that identifier in our dictionary of items.
	if ( [itemIdentifier isEqualToString:TOOLBARITEM_STATEINFO] ) {
		NSToolbarItem* stateItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier] autorelease];
		[stateItem setView:UserInfoView];
		[stateItem setMinSize:NSMakeSize(140, [UserInfoView frame].size.height)];
		[stateItem setMaxSize:NSMakeSize(1000, [UserInfoView frame].size.height)];
		
		return stateItem;
	}
    NSToolbarItem *newItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier] autorelease];
    return newItem;
}

// This method is required of NSToolbar delegates.  It returns an array holding identifiers for the default
// set of toolbar items.  It can also be called by the customization palette to display the default toolbar.    
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
    return [NSArray arrayWithObjects:TOOLBARITEM_STATEINFO,nil];
}

// This method is required of NSToolbar delegates.  It returns an array holding identifiers for all allowed
// toolbar items in this toolbar.  Any not listed here will not be available in the customization palette.
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
    return [NSArray arrayWithObjects:TOOLBARITEM_STATEINFO,nil];
}


- (void)setMainWindowController:(MainWindowController*)p
{
	mainWindowCtrl = [p retain];
}

- (void)setMainAppController:(MainAppController*)p
{
	mainAppCtrl = [p retain];
	[self setMyInfo];
}

- (void)setMyInfo
{
	[NickName setStringValue:[[mainAppCtrl getFxData] getMyNickName]];
	[Impresa setStringValue:[[mainAppCtrl getFxData] getMyImpresa]];
	
	int state = [[mainAppCtrl getFxData] getMyStatus];
	switch(state)
	{
		case FX_STATUS_ONLINE:
			[[statesList itemAtIndex:1] setState:NSOnState];
			activeItem = [statesList itemAtIndex:1];
			[stateImage setImage:[NSImage imageNamed:@"available"]];
			[statesList setTitle:@"available"];
			break;
		case FX_STATUS_BUSY:
			[[statesList itemAtIndex:3] setState:NSOnState];
			activeItem = [statesList itemAtIndex:3];
			[stateImage setImage:[NSImage imageNamed:@"busy"]];
			[statesList setTitle:@"busy"];
			break;
		case FX_STATUS_MEETING:
			[[statesList itemAtIndex:4] setState:NSOnState];
			activeItem = [statesList itemAtIndex:4];
			[stateImage setImage:[NSImage imageNamed:@"meeting"]];
			[statesList setTitle:@"meeting"];
			break;
		case FX_STATUS_PHONE:
			[[statesList itemAtIndex:5] setState:NSOnState];
			activeItem = [statesList itemAtIndex:5];
			[stateImage setImage:[NSImage imageNamed:@"phonecall"]];
			[statesList setTitle:@"phonecall"];
			break;
		case FX_STATUS_AWAY:
			[[statesList itemAtIndex:7] setState:NSOnState];
			activeItem = [statesList itemAtIndex:7];
			[stateImage setImage:[NSImage imageNamed:@"dinner"]];
			[statesList setTitle:@"away"];
			break;
		case FX_STATUS_DINNER:
			[[statesList itemAtIndex:8] setState:NSOnState];
			activeItem = [statesList itemAtIndex:8];
			[stateImage setImage:[NSImage imageNamed:@"dinner"]];
			[statesList setTitle:@"dinner"];
			break;
		case FX_STATUS_EXTENDED_AWAY:
			[[statesList itemAtIndex:9] setState:NSOnState];
			activeItem = [statesList itemAtIndex:9];
			[stateImage setImage:[NSImage imageNamed:@"offsite"]];
			[statesList setTitle:@"offsite"];
			break;
		default:
			break;
	}
}


- (void)updateViewByStatus:(FXSysMessage*)sysMessage
{
	int status = (int)[sysMessage getWParam];
	switch(status)
	{
		case FX_STATUS_ONLINE:
			[activeItem setState:NSOffState];
			[[statesList itemAtIndex:1] setState:NSOnState];
			activeItem = [statesList itemAtIndex:1];
			[stateImage setImage:[NSImage imageNamed:@"available"]];
			[statesList setTitle:@"available"];
			break;
		case FX_STATUS_BUSY:
			[activeItem setState:NSOffState];
			[[statesList itemAtIndex:3] setState:NSOnState];
			activeItem = [statesList itemAtIndex:3];
			[stateImage setImage:[NSImage imageNamed:@"busy"]];
			[statesList setTitle:@"busy"];
			break;
		case FX_STATUS_MEETING:
			[activeItem setState:NSOffState];
			[[statesList itemAtIndex:4] setState:NSOnState];
			activeItem = [statesList itemAtIndex:4];
			[stateImage setImage:[NSImage imageNamed:@"meeting"]];
			[statesList setTitle:@"meeting"];
			break;
		case FX_STATUS_PHONE:
			[activeItem setState:NSOffState];
			[[statesList itemAtIndex:5] setState:NSOnState];
			activeItem = [statesList itemAtIndex:5];
			[stateImage setImage:[NSImage imageNamed:@"phonecall"]];
			[statesList setTitle:@"phonecall"];
			break;
		case FX_STATUS_AWAY:
			[activeItem setState:NSOffState];
			[[statesList itemAtIndex:7] setState:NSOnState];
			activeItem = [statesList itemAtIndex:7];
			[stateImage setImage:[NSImage imageNamed:@"dinner"]];
			[statesList setTitle:@"away"];
			break;
		case FX_STATUS_DINNER:
			[activeItem setState:NSOffState];
			[[statesList itemAtIndex:8] setState:NSOnState];
			activeItem = [statesList itemAtIndex:8];
			[stateImage setImage:[NSImage imageNamed:@"dinner"]];
			[statesList setTitle:@"dinner"];
			break;
		case FX_STATUS_EXTENDED_AWAY:
			[activeItem setState:NSOffState];
			[[statesList itemAtIndex:9] setState:NSOnState];
			activeItem = [statesList itemAtIndex:9];
			[stateImage setImage:[NSImage imageNamed:@"offsite"]];
			[statesList setTitle:@"offsite"];
			break;
		case FX_STATUS_OFFLINE:
			[activeItem setState:NSOffState];
			[[statesList itemAtIndex:11] setState:NSOnState];
			activeItem = [statesList itemAtIndex:11];
			[stateImage setImage:[NSImage imageNamed:@"invisible"]];
			[statesList setTitle:@"invisible"];
			break;
		default:
			break;
	}
	
}

- (IBAction)hostChangeImpresa:(id)sender
{
	NSTextField* textField = (NSTextField*)sender;
	[[mainAppCtrl getFxData] changeHostImpresaTo:[textField stringValue]];
	NSLog(@"change to %@", [textField stringValue]);
}

- (void)stateChangeTo:(NSString*)status
{
	if ([status isEqualToString:@"available"])
	{
		NSLog(@"%@", status);
		//fx_set_user_state(FX_STATUS_ONLINE, NULL, NULL, NULL);
		[[mainAppCtrl getFxData] changeHostStatusTo:FX_STATUS_ONLINE];
	}
	else if ([status isEqualToString:@"busy"])
	{
		NSLog(@"%@", status);
		//fx_set_user_state(FX_STATUS_BUSY, NULL, NULL, NULL);
		[[mainAppCtrl getFxData] changeHostStatusTo:FX_STATUS_BUSY];
	}
	else if ([status isEqualToString:@"meeting"])
	{
		NSLog(@"%@", status);
		//fx_set_user_state(FX_STATUS_MEETING, NULL, NULL, NULL);
		[[mainAppCtrl getFxData] changeHostStatusTo:FX_STATUS_MEETING];
	}
	else if ([status isEqualToString:@"phonecall"])
	{
		NSLog(@"%@", status);
		//fx_set_user_state(FX_STATUS_PHONE, NULL, NULL, NULL);
		[[mainAppCtrl getFxData] changeHostStatusTo:FX_STATUS_PHONE];
	}
	else if ([status isEqualToString:@"away"])
	{
		NSLog(@"%@", status);
		//fx_set_user_state(FX_STATUS_AWAY, NULL, NULL, NULL);
		[[mainAppCtrl getFxData] changeHostStatusTo:FX_STATUS_AWAY];
	}
	else if ([status isEqualToString:@"dinner"])
	{
		NSLog(@"%@", status);
		//fx_set_user_state(FX_STATUS_DINNER, NULL, NULL, NULL);
		[[mainAppCtrl getFxData] changeHostStatusTo:FX_STATUS_DINNER];
	}
	else if ([status isEqualToString:@"offsite"])
	{
		NSLog(@"%@", status);
		//fx_set_user_state(FX_STATUS_EXTENDED_AWAY, NULL, NULL, NULL);
		[[mainAppCtrl getFxData] changeHostStatusTo:FX_STATUS_EXTENDED_AWAY];
	}
	else if ([status isEqualToString:@"invisible"])
	{
		NSLog(@"%@", status);
		//fx_set_user_state(FX_STATUS_OFFLINE, NULL, NULL, NULL);
		[[mainAppCtrl getFxData] changeHostStatusTo:FX_STATUS_OFFLINE];
	}
}
@end
